/*
 *  copyright - (c) 2012/2/17 xueyang
 *  <blunsmith@gmail.com>
 *  <xueyang1122@gmail.com>
 */

#define rULCON1 	(*(volatile unsigned *)(0x7F005400)) 
#define rUCON1 		(*(volatile unsigned *)(0x7F005404)) 
#define rUFCON1 	(*(volatile unsigned *)(0x7F005408)) 
#define rUMCON1 	(*(volatile unsigned *)(0x7F00540C)) 
#define rUTRSTAT1 	(*(volatile unsigned *)(0x7F005410)) 
#define rUERSTAT1 	(*(volatile unsigned *)(0x7F005414)) 
#define rUFSTAT1 	(*(volatile unsigned *)(0x7F005418)) 
#define rUMSTAT1	(*(volatile unsigned *)(0x7F00541C)) 
#define rUTXH1 		(*(volatile unsigned *)(0x7F005420)) 
#define rURXH1		(*(volatile unsigned *)(0x7F005424)) 
#define rUBRDIV1 	(*(volatile unsigned *)(0x7F005428)) 
#define rUDIVSLOT1 	(*(volatile unsigned *)(0x7F00542C)) 
#define rUINTP1 	(*(volatile unsigned *)(0x7F005430)) 
#define rUINTSP1 	(*(volatile unsigned *)(0x7F005434)) 
#define rUINTM1 	(*(volatile unsigned *)(0x7F005438)) 
#define rGPACON0 	(*(volatile unsigned *)(0x7F008000)) 
#define rGPAPUD		(*(volatile unsigned *)(0x7F008008))

#define rGPKPUD         (*(volatile unsigned *)(0x7F00880C))
#define rGPKDAT         (*(volatile unsigned *)(0x7F008808))
#define rGPKCON1        (*(volatile unsigned *)(0x7F008804))
#define rGPKCON0        (*(volatile unsigned *)(0x7F008800)) 

#define rCLKDIVN      (*(volatile unsigned *)0X7E00F020)
#define rMPLLCON      (*(volatile unsigned *)0x7E00F010)


static void init_uart0(void);
static void Tx(char word);
static char Rx();
static void delay(unsigned int z);
static void put_word(char *s);
static void get_word(char *s);
static int led();
char array[512];
int main(void)
{
	
	init_uart0();
	while(1){
		led();
		put_word("helloword\n\r");
//		get_word(array);
	}
	return 0;
}
static void init_uart0(void) 
{
//	rGPAPUD = 0xAAAA;
	rGPACON0 = (rGPACON0 & ~(0xFFFFU<<16)|(0x22222222U)); //set GPACON to UART1
	rULCON1 = (rULCON1 & ~(0xFFFFU)|(0x0003U));	//set word length 8 bit
	rUCON1 = (rUCON1 & ~(0xFU)|(0x5U));	//set transmit/receive mode DMA request
	rUFCON1 = (rUFCON1 & ~(0xFFU)|(0x0U));	//set Tx/Rx FIFO Trigger Level 16 bit 
	rUBRDIV1 = (rUBRDIV1 & ~(0xFFFFU)|(0x21U));	//set baud rate 
//	rUDIVSLOT1 = (rUDIVSLOT1 & ~(0xFFFFU)|(0xDDD5U));	//select the slot where clock generator divide clock source
}

static void Tx(char word) {
	while(!(rUTRSTAT1 & (0x2U)));
	rUTXH1 = word;
}

static char Rx() {
	char word;
	if((rUTRSTAT1 & 0x1)==0x1) {
			word = rURXH1;
	}
	return word;
}
static void delay(unsigned int z) {
	unsigned int x,y;
	for(x=z;x>0;x--)
		for(y=1000;y>0;y--);
}

static void put_word(char *s) {
	while(*s){
		Tx(*s);
		s++;
	}
}
static void get_word(char *s) {
	unsigned char words;
	while(1) {	
		words=Rx();
		if(words == '\r'||words == '\n'){
			*s = 0;
			break;
		}
		*s = words;
		*s++;
	}
}
static int led()
{
	rGPKCON0 = (rGPKCON0 & ~(0xffffU<<16))|(0x1111U<<16);
	rGPKPUD  = (rGPKPUD  & ~(0xffU << 8))|(0x00U<<8);
		int data = 0x09;
		rGPKDAT = (rGPKDAT & ~(0xf<<4)) | ((data & 0xf)<<4);
		delay(2000);
		data = 0x06;
		rGPKDAT = (rGPKDAT & ~(0xf<<4)) | ((data & 0xf)<<4);
		delay(2000);
}
/*
static void delay_1(void)
{
	unsigned int k;
	for(k=0;k<200000;k++);
}
*/
